package com.way.designpatterncodepratice.singleton;

import java.util.concurrent.*;

/**
 * @author liangliwei 80551025
 * @description
 * @date 2020-06-22
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("=====饿汉=======");
        HungrySingleton hungrySingleton1 = HungrySingleton.getInstance();
        HungrySingleton hungrySingleton2 = HungrySingleton.getInstance();
        System.out.println(hungrySingleton1.hashCode());
        System.out.println(hungrySingleton2.hashCode());
        System.out.println(hungrySingleton1 == hungrySingleton2);

        System.out.println("======饿汉 静态代码块======");

        HungrySingletonStaticBlock hungryManStaticBlock1 = HungrySingletonStaticBlock.getInstance();
        HungrySingletonStaticBlock hungryManStaticBlock2 = HungrySingletonStaticBlock.getInstance();
        System.out.println(hungryManStaticBlock1.hashCode());
        System.out.println(hungryManStaticBlock2.hashCode());
        System.out.println(hungryManStaticBlock1 == hungryManStaticBlock2);

        System.out.println("======懒汉 线程不安全======");
        LazySingletonUnsafe lazySingletonUnsafe1 = LazySingletonUnsafe.getInstance();
        LazySingletonUnsafe lazySingletonUnsafe2 = LazySingletonUnsafe.getInstance();
        System.out.println(lazySingletonUnsafe1.hashCode());
        System.out.println(lazySingletonUnsafe2.hashCode());
        System.out.println(lazySingletonUnsafe1 == lazySingletonUnsafe2);

//        ExecutorService threadPool = Executors.newCachedThreadPool();
//        for (int i = 0; i < 10240; i++) {
//            threadPool.execute(() -> System.out.println(Thread.currentThread().getName()+":"+LazySingletonUnsafe.getInstance().hashCode()));
//        }
//        threadPool.shutdown();


        System.out.println("======懒汉 线程安全======");
        LazySingletonSafe lazySingletonSafe1 = LazySingletonSafe.getInstance();
        LazySingletonSafe lazySingletonSafe2 = LazySingletonSafe.getInstance();
        System.out.println(lazySingletonSafe1.hashCode());
        System.out.println(lazySingletonSafe2.hashCode());
        System.out.println(lazySingletonSafe1 == lazySingletonSafe2);


        System.out.println("======静态内部类 线程安全 推荐======");
        StaticInnerSingleton staticInnerSingleton1 = StaticInnerSingleton.InnerClass.getInstance();
        StaticInnerSingleton staticInnerSingleton2 = StaticInnerSingleton.InnerClass.getInstance();
        System.out.println(staticInnerSingleton1.hashCode());
        System.out.println(staticInnerSingleton2.hashCode());
        System.out.println(staticInnerSingleton1==staticInnerSingleton2);


        System.out.println("======枚举 线程安全 推荐======");
        EnumSingleton enumSingleton1 = EnumSingleton.INSTANCE.getInstance();
        EnumSingleton enumSingleton2 = EnumSingleton.INSTANCE.getInstance();
        EnumSingleton enumSingleton3 = EnumSingleton.INSTANCE;
        System.out.println(enumSingleton1.hashCode());
        System.out.println(enumSingleton2.hashCode());
        System.out.println(enumSingleton3.hashCode());
        System.out.println(enumSingleton1 == enumSingleton2);
        System.out.println(enumSingleton1 == enumSingleton3);




    }
}
